package com.dayuanit.booksystem;

import com.dayuanit.booksystem.util.JDBCUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DataBaseUtil3 {

    private static Connection connection;

    private static final String jdbcUrl = JDBCUtils.getUrl();
    private static final String username = JDBCUtils.getUsername();
    private static final String password = JDBCUtils.getPassword();

    static {
        try {
            connection = DriverManager.getConnection(jdbcUrl, username, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 统一修改语句
     * @param sql
     */
    public static void modify(String sql) {
        System.out.println(sql);
        try (Statement statement = connection.createStatement()) {
            int rows = statement.executeUpdate(sql);
            System.out.println("影响行数:" + rows);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static String format(String fieldName) {
        char[] chars = fieldName.toCharArray();
        chars[0] = String.valueOf(chars[0]).toUpperCase().toCharArray()[0];
        return new String(chars);
    }

    public static <T> List<T> commonQueryList(String sql, Class<T> claz, Map<String, String> fieldMap) {
        System.out.println(sql);
        //此集合是用来存放查询出来的对象
        List<T> list = new ArrayList<>();

        //得到类的所有成员变量
        Field[] fields = claz.getDeclaredFields();

        try (Statement statement = connection.createStatement()) {
            ResultSet rs = statement.executeQuery(sql);
            //循环结果集的每一行
            while (rs.next()) {
                T t = claz.newInstance();

                //循环当前行的每一列 for循环结束后，那么对象t的成员变量就有值了
                for (Field field : fields) {
                    if (field.getType() == String.class) {
                        //当前列的值
                        String value = rs.getString(fieldMap.get(field.getName()));

                        //找到当前属性的对应的set方法，通过反射调用之
                        Method method = claz.getMethod("set" + format(field.getName()), String.class);
                        method.invoke(t, value);//t.setId(value);
                    }

                    if (field.getType() == long.class) {
                        //当前列的值
                        long value = rs.getLong(fieldMap.get(field.getName()));

                        //找到当前属性的对应的set方法，通过反射调用之
                        Method method = claz.getMethod("set" + format(field.getName()), long.class);
                        method.invoke(t, value);//t.setId(value);
                    }

                    //TODO .... 正常情况下 应该有9个if判断，八个基本类型 和 String
                }

                //将本次循环的结果产生的对象t放到集合中
                list.add(t);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 查询单条记录
     * @param sql
     * @param claz
     * @param <T>
     * @return
     */
    public static <T> T commonQuery(String sql, Class<T> claz, Map<String, String> fieldMap) {
        List<T> list = commonQueryList(sql, claz, fieldMap);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }

    public static void main(String[] args) {



    }



}
